<template>
	<view>
		<view class="header">
			<image src="/static/left.png" class="back" @click="back"></image>
			<text class="header_title">企业信息</text>
			<text class="header_edit" @click="edit">{{editbutton}}</text>
		</view>

		<scroll-view scroll-y="true" style="height: 90vh;">
			<view class="view1" v-for="(item,index) in infoList">
				<view style="display: flex;flex-direction: column;" v-for="(itemList,itemIndex) in item.list">
					<view class="view">
						<text class="title">{{itemList.title}}</text>
						<input class="input" :placeholder="itemList.value" v-model="itemList.value"
							:disabled="index == 1 && itemIndex == 1 || itemIndex == 3 || itemIndex == 4?!isedit:'true'"
							:style="{color:isedit && index == 1 && itemIndex == 1 | itemIndex == 3 | itemIndex == 4? '#333333':'#999999'}" />
						<image v-if="isedit && itemList.title == '坐标'" src="../../../static/address.png"
							style="width: 46rpx;height: 46rpx;margin-left: 17rpx;" @click="map"></image>
					</view>
					<view v-if="index != 0"
						style="height: 1rpx;background-color: #EDEDED;margin-left: 24rpx;margin-right: 24rpx;"></view>
				</view>

			</view>
		</scroll-view>
		<suspended @userData="childData"></suspended>
	</view>
</template>

<script>
	import {
		dwxx,
		dwxxEdit
	} from "../../../common/api.js"
	export default {
		data() {
			return {
				editbutton: '编辑',
				isedit: false,

				infoList: [{
					list: [{
						title: '节水代码',
						value: ''
					}],
				}, {
					list: [{
							title: '单位名称',
							value: ''
						},
						{
							title: '单位地址',
							value: ''
						},
						{
							title: '邮编',
							value: ''
						},
						// {
						// 	title: '电话',
						// 	value: ''
						// },
						{
							title: '联系人',
							value: ''
						},
						{
							title: '手机号码',
							value: ''
						}
					],
				}, {
					list: [{
							title: '所属系统',
							value: ''
						},
						{
							title: '所属部门',
							value: ''
						},
						{
							title: '国标代码',
							value: ''
						},
						{
							title: '电子邮箱一',
							value: ''
						},
						{
							title: '电子邮箱二',
							value: ''
						},
						{
							title: '行政区划',
							value: ''
						},
						{
							title: '单位人数(人)',
							value: ''
						},
						{
							title: '营业面积(㎡)',
							value: ''
						},
						{
							title: '用水设备',
							value: ''
						},
						{
							title: '用水工艺',
							value: ''
						},
						{
							title: '坐标',
							value: ''
						},
						{
							title: '统一社会信用代码',
							value: ''
						}
					]
				}],

				id: null,

				dwdz: '',
				lxr: '',
				sjhm: ''
			}
		},

		onLoad() {
			this.getInformation()
		},

		onShow() {
			let that = this
			uni.$on('returnData', function(data) {
				that.infoList[2].list[10].value = data
				console.log('监听到事件来自返回的参数：' + data);
			})
		},


		methods: {

			//截取字符串
			/* 部分隐藏处理
			 ** oldName 需要处理的字符串
			 ** frontLen 保留的前几位
			 ** endLen 保留的后几位
			 ** cha 替换的字符串
			 */
			plusXing(oldName, frontLen, endLen, cha) {
				console.log(oldName)
				let len = oldName.length - frontLen - endLen;
				let xing = "";
				for (let i = 0; i < len; i++) {
					xing += cha;
				}
				return (
					oldName.substring(0, frontLen) + xing + oldName.substring(oldName.length - endLen)
				);
			},


			back() {
				uni.navigateBack()
			},
			edit() {
				if (this.isedit) {
					this.dwxxEdit()
				}
				this.isedit = !this.isedit
				this.editbutton = this.isedit ? '保存' : '编辑'
			},
			map() {
				uni.navigateTo({
					url: '/pages/Services/Map/Map?latlong=' + this.infoList[2].list[10].value
				})
			},

			async getInformation() {
				var data = {
					page: "1",
					size: "10",
					dwtype: "0",
					cxtype: "0",
					nd: new Date().getFullYear()
				}
				let res = await dwxx(data)

				var information = res.response.data[0]
				this.id = information.Id
				this.infoList[0].list[0].value = information.jsdm
				this.infoList[1].list[0].value = information.dwmc
				this.infoList[1].list[1].value = information.dwdz.length > 12 ? this.plusXing(information.dwdz, 6, 0,
					"*") : this.plusXing(information.dwdz, information.dwdz.length / 2, 0, "*")
				this.dwdz = information.dwdz
				this.infoList[1].list[2].value = information.dwyb
				//this.infoList[1].list[3].value = information.dwdh
				this.infoList[1].list[3].value = this.plusXing(information.lxr, 0, 1, "*")
				this.lxr = information.lxr
				this.infoList[1].list[4].value = this.plusXing(information.sjhm, 3, 2, "*")
				this.sjhm = information.sjhm
				this.infoList[2].list[0].value = information.xt
				this.infoList[2].list[1].value = information.ssbm
				this.infoList[2].list[2].value = information.gbdm
				if (information.email) {
					this.infoList[2].list[3].value = this.plusXing(information.email.split('@')[0], 0, 0, "*") + '@' +
						information.email.split('@')[1]
				}
				if (information.email2) {
					this.infoList[2].list[4].value = this.plusXing(information.email2.split('@')[0], 0, 0, "*") + '@' +
						information.email2.split('@')[1]
				}
				this.infoList[2].list[5].value = information.xzqh
				this.infoList[2].list[6].value = information.dwrs
				this.infoList[2].list[7].value = information.yymj
				this.infoList[2].list[8].value = information.yssb
				this.infoList[2].list[9].value = information.ysgy
				this.infoList[2].list[10].value = information.coordinate
				this.infoList[2].list[11].value = information.creditcode


			},

			async dwxxEdit() {

				var data = {
					//"dwmc": this.infoList[1].list[0].value,
					"dwdz": this.infoList[1].list[1].value,
					//"dwyb": this.infoList[1].list[2].value,
					//"dwdh": this.infoList[1].list[3].value,
					//"ssbm": this.infoList[2].list[1].value,
					"lxr": this.infoList[1].list[3].value,
					// "xt": this.infoList[2].list[0].value,
					// "gbdm": this.infoList[2].list[2].value,
					// "email": this.infoList[2].list[3].value,
					// "email2": this.infoList[2].list[4].value,
					"sjhm": this.infoList[1].list[4].value,
					// "xzqh": this.infoList[2].list[5].value,
					// "dwrs": parseInt(this.infoList[2].list[6].value),
					// "yymj": parseInt(this.infoList[2].list[7].value),
					// "yssb": this.infoList[2].list[8].value,
					// "ysgy": this.infoList[2].list[9].value,
					// "coordinate": this.infoList[2].list[10].value,
					// "creditcode": this.infoList[2].list[11].value,
					"Id": this.id,
				}

				if (data.dwdz == this.dwdz.length > 12 ? this.plusXing(this.dwdz, 6,
						0,
						"*") : this.plusXing(this.dwdz, this.dwdz.length / 2, 0, "*")) {
					data.dwdz = this.dwdz
				}

				if (data.lxr == this.plusXing(this.lxr, 0, 1, "*")) {
					data.lxr = this.lxr
				}

				if (data.sjhm == this.plusXing(this.sjhm, 3, 2, "*")) {
					data.sjhm = this.sjhm
				}
				
				console.log('保存：' + data);
				let res = await dwxxEdit(data)

				if (res.status == 200) {
					uni.showToast({
						title: '保存成功',
						icon: "success",
						duration: 2000
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: "none",
						duration: 2000
					})
				}
			},

			childData(param) {
				console.log('切换节水代码', param)
				this.getInformation()
			}
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		background-color: #F4F4F4;
	}

	.header {
		height: 88rpx;
		padding-left: 12px;
		padding-right: 24rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-top: 0rpx;
		background-color: white;
	}

	.back {
		width: 23px;
		height: 23px;
	}

	.header_title {
		font-size: 36rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
		line-height: 50rpx;
		flex: 1;
		text-align: center;
	}

	.header_edit {
		font-size: 34rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #1677FF;
		line-height: 48rpx;
	}

	.view {
		background: #FFFFFF;
		border-radius: 16rpx;
		padding: 29rpx 24rpx 29rpx 24rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.title {
		font-size: 30rpx;
		font-family: MicrosoftYaHei;
		color: #333333;
		line-height: 42rpx;
	}

	.input {
		font-size: 30rpx;
		font-family: Regular;
		color: #333333;
		line-height: 42rpx;
		flex: 1;
		text-align: right;

		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 5; //可设置显示的行数
		line-clamp: 6;
		-webkit-box-orient: vertical;
	}

	.view1 {
		background: #FFFFFF;
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;
		margin: 24rpx 24rpx 24rpx 24rpx;
	}
</style>
